package com.baby.mall.product.service.impl;

import com.baby.mall.product.entity.AttrEntity;
import com.baby.mall.product.entity.vo.AttrGroupWithAttrsVo;
import com.baby.mall.product.service.AttrService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baby.common.utils.PageUtils;
import com.baby.common.utils.Query;

import com.baby.mall.product.dao.AttrGroupDao;
import com.baby.mall.product.entity.AttrGroupEntity;
import com.baby.mall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据id查询出属性分组
     *
     * @param params    :
     * @param catelogId :
     * @Author QLZ
     * @Date 2021/3/16
     * @return: com.baby.common.utils.PageUtils
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key=(String)params.get("key") ;
        QueryWrapper<AttrGroupEntity> queryWrapper=new QueryWrapper<AttrGroupEntity>();
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key).or();
            });
        }

        if(catelogId==0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper);
            return new PageUtils(page);
        }else {
            queryWrapper.eq("catelog_id",catelogId);

            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 根据分类id查找所有规格参数及分组
     *
     * @param catelogId :
     * @Author QLZ
     * @Date 2021/3/17
     * @return: java.util.List<com.baby.mall.product.entity.vo.AttrGroupWithAttrsVo>
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelog(String catelogId) {
        //1.查询分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //2.查询所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(item -> {
            AttrGroupWithAttrsVo groupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item,groupWithAttrsVo);
            List<AttrEntity> relationAttr = attrService.getRelationAttr(groupWithAttrsVo.getAttrGroupId());
            groupWithAttrsVo.setAttrs(relationAttr);
            return groupWithAttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

}